﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SSISLib
{
    public partial class SharePointFileConfigForm : Form
    {
        /// <summary>
        /// Constructor
        /// </summary>
        public SharePointFileConfigForm(System.Collections.Hashtable properties, bool bWriteMode, string portID, BlueIntegrator.DAL.DALBase dal)
        {
            InitializeComponent();
            m_properties = properties;
            m_portId = portID;
            m_dal = dal;

            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            bf.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;
            bf.TypeFormat = System.Runtime.Serialization.Formatters.FormatterTypeStyle.TypesWhenNeeded;

            this.userTextBox.Text = properties["SPUser"] as string ?? "";
            this.urlTextBox.Text = properties["SPURL"] as string ?? "";
            this.orgTextBox.Text = properties["SPList"] as string ?? "";
            this.pwdTextBox.Text = BlueIntegrator.Helpers.Encryption.DecryptString(properties["SPPwd"] as string ?? "", "SSIS");
            this.timeoutTextBox.Text = SafeGetInt(properties["SPTimeOut"], 100).ToString();
        }

        /// <summary>
        /// Conversion helper
        /// </summary>
        /// <param name="v"></param>
        /// <returns></returns>
        private int SafeGetInt(object v)
        {
            return SafeGetInt(v, 0);
        }

        /// <summary>
        /// Conversion helper
        /// </summary>
        /// <param name="v"></param>
        /// <returns></returns>
        private int SafeGetInt(object v, int def)
        {
            if (v == null)
                return def;
            
            try
            {
                return System.Convert.ToInt32(v);
            }
            catch
            {
            }

            return def;
        }

        /// <summary>
        /// Property helper
        /// </summary>
        /// <param name="propname"></param>
        /// <returns></returns>
        private object GetPropertyHelper(string propname)
        {
            object val = m_properties[propname];
            return val ?? "";
        }

        /// <summary>
        /// Property helper
        /// </summary>
        /// <param name="propname"></param>
        /// <returns></returns>
        private void SetPropertyHelper(string propname, object val)
        {
            m_properties[propname] = val;
        }

        System.Collections.Hashtable m_properties = null;
        string m_portId;
        BlueIntegrator.DAL.DALBase m_dal = null;

        /// <summary>
        /// Cancel handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Validate and save
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.urlTextBox.Text == "")
                {
                    MessageBox.Show("You must enter a SharePoint server URL.", "BlueIntegrator", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    this.urlTextBox.Focus();
                    this.tabControl1.SelectedIndex = 0;
                    return;
                }

                SharePointListAPI api = new SharePointListAPI();

                if (this.timeoutTextBox.Text == "")
                    this.timeoutTextBox.Text = "60";

                int timeOut;

                if (!int.TryParse(this.timeoutTextBox.Text, out timeOut))
                {
                    this.timeoutTextBox.Text = "60";
                }

                try
                {
                    System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
                    api.GetColumns(this.orgTextBox.Text, this.urlTextBox.Text, this.userTextBox.Text, this.pwdTextBox.Text, timeOut, m_dal);
                }
                catch (System.Exception exc)
                {
                    MessageBox.Show("An error occurred connecting to the SharePoint server : " + exc.ToString() + "\r\n\r\nPlease check the connection parameters.", "BlueIntegrator", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    this.urlTextBox.Focus();
                    this.tabControl1.SelectedIndex = 0;
                    return;
                }
                finally
                {
                    System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
                }

                List<SharePointListAPI.Binding> result = new List<SharePointListAPI.Binding>();

                m_properties["SPURL"] = this.urlTextBox.Text;
                m_properties["SPList"] = this.orgTextBox.Text;
                m_properties["SPUser"] = this.userTextBox.Text;
                m_properties["SPPwd"] = BlueIntegrator.Helpers.Encryption.EncryptString(this.pwdTextBox.Text, "SSIS");

                if (!int.TryParse(this.timeoutTextBox.Text, out timeOut))
                    timeOut = 60;

                m_properties["SPTimeOut"] = timeOut;

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (System.Exception exc)
            {
                MessageBox.Show("Error : " + exc.ToString(), "BlueIntegrator", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }
    }
}
